﻿import QtQuick 2.0
import QtQuick.Window 2.12
import "../components"
Rectangle {
    property int connectType: 0
    property bool isClicked: false //点击事件触发

    radius: 5*dp

    Connections{
        target: CSignal
        onCommunicationStatus:{
            if(status==0 && isClicked){
                isClicked=false
                connToast.show("连接成功",0)
            }
            else if(status==1 && isClicked){
                connToast.show("连接失败",1)
            }
        }
    }
    Component.onCompleted: {
        var buttonList = row_button.children
        for(let i = 0; i < buttonList.length; i ++){
            buttonList[i].button_color = "transparent"
            buttonList[i].button_color_select = "white"
            buttonList[i].radius = 4*dp
        }
    }
    onVisibleChanged: {
        if(visible){
            connectType = GetMessageInterface.getDefaultConnectInfo()
            var data=GetMessageInterface.getConnectInfo(connectType);
            ip.setText(data["Ip"])
            port.setText(data["Port"])
        }
    }

    Toast{
        id: connToast
        y: 150*dp
        x: parent.width / 2 - 150*dp
    }

    // 按钮 ================================================
    Rectangle{
        color: "#EBEEF3"
        anchors.horizontalCenter: parent.horizontalCenter
        y: 60*dp
        height: 54*dp; width: 334*dp
        radius: 4*dp
        Row{
            id: row_button
            x: 2*dp; y: 2*dp
            spacing: 0
            PushButton{
                title: "UDP通信"
                width: 110*dp; height: 50*dp
                isSelected: connectType === 0
                onReleased: {
                    connectType = 0
                    var data=GetMessageInterface.getConnectInfo(0);
                    ip.setText(data["Ip"])
                    port.setText(data["Port"])
                }
            }
            //            PushButton{
            //                title: "TCP通信"
            //                width: 110*dp; height: 50*dp
            //                isSelected: connectType === 1
            //                onReleased: {
            //                    connectType = 1
            //                    var data=GetMessageInterface.getConnectInfo(1);
            //                    ip.setText(data["Ip"])
            //                    port.setText(data["Port"])
            //                }
            //            }
            //            PushButton{
            //                title: "USB通信"
            //                width: 110*dp; height: 50*dp
            //                isSelected: connectType === 2
            //                onReleased: {
            //                    connectType = 2
            //                    var data = GetMessageInterface.getConnectInfo(2);
            //                    ip.setText(data["Ip"])
            //                    port.setText(data["Port"])
            //                }
            //            }
        }

    }
    // 文本框 =============================================
    Item{
        y: 200*dp
        anchors.horizontalCenter: parent.horizontalCenter
        width: 260*dp; height: 500*dp
        Column{
            anchors.fill: parent
            spacing: 20*dp
            CustomInputText{
                id: ip
                required: true
                title: connectType === 2 ? "串口号" : "IP地址"
                defaultText: "127.0.0.1"
            }
            CustomInputText{
                id: port
                required: true
                title: connectType === 2 ? "波特率" : "端口号"
                defaultText: "8999"
            }
            Rectangle { height: 20*dp; width: 20*dp }
            PushButton{
                title: "连接"
                anchors.horizontalCenter: parent.horizontalCenter
                defaultMode1: true
                onReleased: {
                    isClicked=true
                    GetMessageInterface.connectEqu(connectType,ip.getText(), port.getText())
                }
            }
        }
    }
}
